package org.lql.algo.codecrush.hot100.matrix;

/**
 * @author: liangqinglong
 * @date: 2025-10-11 16:41
 * @description: 48. 旋转图像 <a href="https://leetcode.cn/problems/rotate-image/description/?envType=study-plan-v2&envId=top-100-liked">...</a>
 **/
public class Rotate {

	public void rotate(int[][] matrix) {
		int n = matrix.length;

		// 1. 转置矩阵
		for (int i = 0; i < n; i++) {
			for (int j = i; j < n; j++) {
				int temp = matrix[i][j];
				matrix[i][j] = matrix[j][i];
				matrix[j][i] = temp;
			}
		}
		print(matrix);

		// 2. 反转每一行
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n / 2; j++) {
				int temp = matrix[i][j];
				matrix[i][j] = matrix[i][n - 1 - j];
				matrix[i][n - 1 - j] = temp;
			}
		}
		print(matrix);
	}

	public static void print(int[][] matrix) {
		System.out.println();
		for (int[] ints : matrix) {
			for (int anInt : ints) {
				System.out.print(anInt + " ");
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {
		int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
		print(matrix);
		new Rotate().rotate(matrix);
//		matrix = new int[][]{{5, 1, 9, 11}, {2, 4, 8, 10}, {13, 3, 6, 7}, {15, 14, 12, 16}};
//		print(matrix);
//		new Rotate().rotate(matrix);
	}
}
